import { Button, Space, Popconfirm, message } from "antd";
import { ControlType as TYPE } from "@components";
import { riskAreaDelete } from "@apis/riskAreaApi";
import { useMainContext } from '@contexts/MainContext'

const selectConfig = {
  controlType: TYPE.SELECT,
  allowClear: true,
  showSearch: true,
  optionFilterProp: "children",
  filterOption: (input, option) => option.children.toLowerCase()?.includes(input.toLowerCase())
}

export const fields = [
  { name: 'unitCode', label: "归属机构", controlType: TYPE.SELECT, codeKey: 'userAuthUnits', ...selectConfig },
  { name: "cfgAreaName", label: "区域名称", controlType: TYPE.INPUT },
  // { name: 'areaType', label: "区域类型", controlType: TYPE.SELECT, codeKey: 'taskType' },
  { name: 'createTime', label: '创建时间', controlType: TYPE.DATE_RANGE_PICKER, linkageFields: ['startTime', 'endTime'], showTime: true },
  { name: 'status', label: "状态", controlType: TYPE.SELECT, codeKey: 'status' },
];

const pass = (row, queryFormRef) => {
  return new Promise(async (resolve, reject) => {
    const {data, error} = await riskAreaDelete(row.cfgAreaId)
    if (error) return reject(error)
    resolve()
    queryFormRef.current.submit()
    message.success(data)
  })
};
export const GetColumns = (queryFormRef, editFun) => {
  const { isButtomShow } = useMainContext()

  return [
    { title: '区域名称', dataIndex: 'cfgAreaName', key: 'cfgAreaName', open: true },
    { title: '归属机构', dataIndex: 'unitName', key: 'unitName', open: true },
    { title: '所属区域', dataIndex: 'address', key: 'address', open: true },
    // { title: '区域类型', dataIndex: 'areaType', key: 'areaType', open: true },
    { title: '创建时间', dataIndex: 'createTime', key: 'createTime', open: true },
    { title: '状态', dataIndex: 'status', key: 'status', open: true },
    {
      title: "操作",
      dataIndex: 'operationCol',
      key: 'operationCol',
      open: true,
      fixed: 'right',
      render: (_, record) => {
        return (
          <Space>
            {isButtomShow('050104') && <Button type="primary" onClick={() => editFun(record)}>编辑</Button>}
            {(isButtomShow('050105') && record.status !== '无效') && (
            <Popconfirm title="是否确定删除该区域？" onConfirm={() => pass(record, queryFormRef)}>
              <Button type="primary" danger>删除</Button>
            </Popconfirm>)}
          </Space>
        )
      }
    }
  ]
}